package cn.itsource.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.ParamDo;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pay.service.AlipayService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.math.BigDecimal;


@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;
    @Autowired
    private AlipayService alipayService;
    @Override
    @Transactional
    public JsonResult submitOrder(ParamDo paramDo) {
        //获取地址userid查地址
        Long userAddressId = paramDo.getAddressId();
        //查地址
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(userAddressId);
        //插入订单地址表
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);
        orderAddressMapper.insertSelective(orderAddress);
        //查宠物表
        Pet pet = petMapper.selectByPrimaryKey(paramDo.getPetId());
        //准备更新订单表
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("宠物订单:"+ JSON.toJSONString(paramDo));
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getSaleprice()));
        orderAdopt.setPetId(paramDo.getPetId());
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        orderAdopt.setOrderSn(orderNo);
        orderAdopt.setUserId(LoginInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setAddressId(orderAddress.getId());
        orderAdoptMapper.insertSelective(orderAdopt);
        return JsonResult.createSuccess("成功",alipayService.createPayUrl(orderNo,pet.getSaleprice(), "宋睿睿"));
    }

    @Override
    public Pet getPrice(ParamDo paramDo) {
        Long petId = paramDo.getPetId();
        return petMapper.getPrice(petId);
    }

    @Override
    public OrderAdopt salePrice(String orderSn) {
        OrderAdopt orderAdopt = orderAdoptMapper.selectByOrderSn(orderSn);
        return orderAdopt;
    }

    @Override
    public OrderAddress address(String orderSn) {
        OrderAdopt orderAdopt = orderAdoptMapper.selectByOrderSn(orderSn);
        Long petId = orderAdopt.getPetId();
        Pet pet = petMapper.selectByPrimaryKey(petId);
        pet.setState(2);
        petMapper.updateByPrimaryKeySelective(pet);
        Long addressId = orderAdopt.getAddressId();
        OrderAddress orderAddress = orderAddressMapper.selectByPrimaryKey(addressId);
        return orderAddress;
    }

    public static void main(String[] args) {
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
    }
}
